<?php
namespace app\spadmin\controller\system;
use app\common\controller\Backend;
use EasyWeChat\Factory;
class Menu extends Backend{

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('WechatMenu');
        $menu = $this->model->where('status',1)->order('sort desc')->paginate(999999)->toArray();

        $this->assign('menu',formatTree(listToTree($menu['data'])));
    }

    public function index(){
        $this->request->filter(['strip_tags', 'trim']);
        if($this->request->isAjax()){
            $data = $this->request->param();
            $list =   $this->model->order('sort desc')->paginate(999999)->toArray();
            return $this->result($list);
        }
        return $this->view->fetch();
    }
    public function send(){
        if($this->request->isPost()){
            $menu = $this->model->where('status',1)->order('sort desc')->select();
            $app = Factory::officialAccount(config('wechat'));
            $buttons = [];
            foreach ($menu as $key => $value) {
                if($value['pid']==0){
                    $current=["name"=>$value['title']];
                    foreach ($menu as $k => $v) {
                        if($v['pid'] == $value['id']){
                            if($v['menu_type'] == '1')
                            $current['sub_button'][] = [
                                "type" => "view",
                                "name" => $v['title'],
                                "url"  => $v['url'],
                                "key"=>'subbtn_'.$v['id'],
                                
                            ];
                        }
                        if($v['pid'] == $value['id']){
                            if($v['menu_type'] == '2')
                            $current['sub_button'][] = [
                                "type" => "scancode_push",
                                "name" => $v['title'],
                                "key"=>'subbtn_'.$v['id'],
                               
                            ];
                        }
                    }
                    $buttons[] = $current;
                }
            }
      
            //$current = $app->menu->current();
            $result = $app->menu->create($buttons);
            $this->success('操作成功','',$result);
        }
        

    }
}